package com.pic.mgt.unit.operation;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Business;
import com.pic.mgt.unit.Unit;
import com.pic.mgt.utils.LogUtils;

public final class SimpleBusinessUnitOperation extends SimpleUnitOperation
											implements BusinessUnitOperation {

	@Override
	public void copyTargetFile(Business business, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: copying target file '{}' to folder '{}'.",
								business.getUnitName(), business.getTargetFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			super.simpleFileOperation.copyFileToFolder(
					business.getTargetFile(), destFolder);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: copying target file '{}' to folder '{}' completed.",
								business.getUnitName(), business.getTargetFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException("Business '"
						+ business.getUnitName()
						+ "' operation: copying target file '"
						+ business.getTargetFilename() + "' to folder '"
						+ destFolder.getAbsolutePath() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void moveTargetFile(Business business, File destFolder)
			throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: moving target file '{}' to folder '{}'.",
								business.getUnitName(), business.getTargetFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
			
			File newTargetFile =
				super.simpleFileOperation.moveFileToFolder(
							business.getTargetFile(), destFolder);
			business.setTargetFile(newTargetFile);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: moving target file '{}' to folder '{}' completed.",
								business.getUnitName(), business.getTargetFilename(), destFolder
										.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException("Business '"
						+ business.getUnitName()
						+ "' operation: moving target file '"
						+ business.getTargetFilename() + "' to folder '"
						+ destFolder.getAbsolutePath() + "' failed.", e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void removeTargetFile(Business business) throws UnitOperationException {
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: removing target file '{}'.",
								business.getUnitName(), business.getTargetFilename());
			}
			// ********************** Log **********************
			
			super.simpleFileOperation.deleteFile(business.getTargetFile());
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: removing target file '{}' completed.",
								business.getUnitName(), business.getTargetFilename());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new UnitOperationException(
						"Business '" + business.getUnitName() +
						"' operation: removing target file '" +
						business.getTargetFilename() +"' failed.",
						e);
			} catch (UnitOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************
				
				throw e1;
			}
		}
	}

	@Override
	public void removeUnit(Unit unit) throws UnitOperationException {
		Business business = (Business)unit;
		
		// 刪除目標檔
		this.removeTargetFile(business);
		
		// 刪除確認檔
		super.removeConfirmationFile(unit);
	}

}
